package kgc.LiuMang.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class EncodingFilter
 */
@WebFilter(urlPatterns = "/*")
public class EncodingFilter implements Filter {

    /**
     * Default constructor. 
     */
    public EncodingFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		System.out.println("===========过滤器销毁===============");
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("===========执行encodingfilter==========");
		//设置请求接受的参数的编码
		request.setCharacterEncoding("utf-8");
		//设置服务器回应的字符编码
		response.setCharacterEncoding("utf-8");
		// 设置服务器回应到页面上的字符编码
		response.setContentType("text/html;charset=utf-8");
		//chain 链条，连续，因为过滤器将请求截断了，需要调用 doFiltr方法将请求连接下去，否则请求会停止
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("==================初始化======================");
	
	}

}
